<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>超级群列表</title>
    <link rel="stylesheet" type="text/css" th:href="@{/js/layui/css/layui.css}" media="all">
    <link rel="stylesheet" type="text/css" th:href="@{/css/fyn/style.css/}" media="all">
    <link rel="stylesheet" type="text/css" th:href="@{/css/fyn/my.css/}" media="all">

</head>
<body class="body">
<script type="text/html" id="topOperate">
    <div class="my-btn-box layui-form">
      <span class="fl">
    <button class="layui-btn layui-btn-sm" lay-event="toSave"><i class="layui-icon layui-icon-add-1"></i>添加</button>
      </span>
    </div>
</script>
<div class="my-btn-box layui-form">
</div>
<!-- 表格 -->
<div id="dateTable" lay-filter="event"></div>
<div class="height50"></div>
<!--新增超级群弹框-->
<div id="ultragroupId" style="display: none;padding: 30px">
    <form class="layui-form" lay-filter="ultragroup">
        <!--<input type="hidden" name="id" lay-verify="id" autocomplete="off" class="layui-input">-->
        <div class="layui-form-item">
            <label class="layui-form-label">用户ID</label>
            <div class="layui-input-block">
                <input type="text" name="userId" lay-verify="userId" autocomplete="off" placeholder="请输入用户ID" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">超级群名称</label>
            <div class="layui-input-block">
                <input type="text" name="groupName" lay-verify="groupName" autocomplete="off" placeholder="请输入超级群名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否审核</label>
            <div class="layui-input-inline">
                <input type="checkbox" lay-verify="required" checked="" lay-filter="state"
                       id="approvalId" name="approval" lay-skin="switch" lay-text="是|否">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">群头像</label>
            <div class="layui-input-block">
                <input class="layui-input" name="avatar" placeholder="群头像" type="text">
            </div>

            <div class="layui-upload">
                <button type="button" class="layui-btn" id="test1">上传</button>
                <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                    预览图：
                    <div class="layui-upload-list" id="demo1"></div>
                </blockquote>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="ultragroupSubmit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<div id="editUltragroupId" style="display: none;padding: 30px">
    <form class="layui-form" lay-filter="ultragroup">
        <input type="hidden" name="ultragroupId" lay-verify="ultragroupId" autocomplete="off" class="layui-input">
        <div class="layui-form-item">
            <label class="layui-form-label">超级群名称</label>
            <div class="layui-input-block">
                <input type="text" name="groupName" lay-verify="groupName" autocomplete="off" placeholder="请输入超级群名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否审核</label>
            <div class="layui-input-inline">
                <input type="checkbox" lay-verify="required" lay-filter="state"
                       id="editApprovalId" name="approval" checked lay-skin="switch" lay-text="是|否">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">群头像</label>
            <div class="layui-input-block">
                <input class="layui-input" name="avatar" placeholder="群头像" type="text">
            </div>

            <div class="layui-upload">
                <button type="button" class="layui-btn" id="test2">上传</button>
                <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                    预览图：
                    <div class="layui-upload-list" id="demo2"></div>
                </blockquote>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="editUltragroupSubmit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script th:src="@{/js/layui/layui.js}"></script>
<script th:src="@{/js/fyn/index.js}"></script>
<script th:src="@{/js/fyn/my.js}"></script>
<script type="text/javascript" th:src="@{/js/vip/vip_comm.js}"></script>
<script type="text/javascript" th:src="@{/js/vip/vip_table.js}"></script>
<script type="text/javascript" th:inline="javascript">

    // layui方法
    layui.use(['table', 'form', 'laypage', 'layer', 'vip_table','laydate','upload'], function () {
        // 操作对象
        var form = layui.form
            , table = layui.table
            , layer = layui.layer
            , upload = layui.upload
            , $ = layui.jquery;

        var imgList = [];
        // 新增执行上传
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            , url: [[${basePath}]] + '/upload/oss'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').html('<img style="width: 50px;height: 50px;" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                });
            }
            , done: function (res) {
                imgList.push(res.data);
                $("input[name='avatar']").val('' + res.data);
                // $("#imgStore").html("<img src='" + res.data+"' />");
            }
            , error: function () {
                //请求异常回调
            }
        });
        // 编辑执行上传
        upload.render({
            elem: '#test2' //绑定元素
            , url: [[${basePath}]] + '/upload/oss'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo2').html('<img style="width: 50px;height: 50px;" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                });
            }
            , done: function (res) {
                imgList.push(res.data);
                $("input[name='avatar']").val('' + res.data);
                // $("#imgStore").html("<img src='" + res.data+"' />");
            }
            , error: function () {
                //请求异常回调
            }
        });

        // 表格渲染
        var tableIns = table.render({
            elem: '#dateTable'                  //指定原始表格元素选择器（推荐id选择器）
            , height: "full"    //容器高度
            // , skin: 'line'
            , even: 'true'
            , cols: [[                  //标题栏
                 {
                    field: 'ultragroupId', title: '超级群ID',minWidth: 100, align: 'center'
                },
                 {
                    field: 'ultragroupIdNum', title: '超级群ID',minWidth: 100, align: 'center'
                }
                , {
                    field: 'ultragroupName', title: '超级群名称', minWidth: 100, align: 'center'
                },
                {field: 'avatar', title: '群头像', minWidth: '80', align: 'center', templet: function (d) {
                        if (d.avatar) {
                            return "<img style=\"width: 50px;height: 50px;\" src=\""+d.avatar+"\" class=\"layui-upload-img\">";
                        }
                        return "";
                }},
                {field: 'approval', title: '审核', minWidth: '20', align: 'center', templet: function (d) {
                        if (d.approval == '0') {
                            return "需要";
                        }
                        return "不需要";
                }},
                {
                    field: 'createTime',
                    align: 'center',
                    title: '发送时间',
                    sort: true,
                    minWidth: 200,
                    templet: function (d) {
                        return new Date(d.createTime).Format("yyyy-MM-dd hh:mm:ss");
                    }
                }
                , {title: '操作', align: 'center', toolbar: '#barOption'} //这里的toolbar值是模板元素的选择器
            ]]
            , id: 'dataCheck'
            , url: [[${basePath}]] + '/ultragroup/queryList'
            , method: 'get'
            , page: false
            , limits: [10, 30, 60, 90, 150, 300]
            , toolbar: '#topOperate'
            , limit: 10 //默认采用0
            , loading: true
            // , where: getJson()
            , done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                $("td[data-field='content']").each(function () {
                    $(this).find(".layui-table-cell").removeClass('layui-table-cell');
                });


            }
        });

        // 新增超级群
        form.on('submit(ultragroupSubmit)', function(data){
            var data = data.field;
            var approval = $('#approvalId').prop('checked') ? 0 : 1;
            $.ajax({
                type: "post",
                async: false,
                url: [[${basePath}]] + "/ultragroup/createUltragroup",
                data: {
                    userId: data.userId,
                    groupName: data.groupName,
                    avatar: data.avatar,
                    approval: approval
                },
                success: function (res) {
                    if (res.success) {
                        layer.msg("创建成功！", {icon: 1}, 2000);
                        layer.closeAll();
                        tableIns.reload();
                    } else {
                        layer.msg(res.msg, {icon: 2}, 2000);
                    }
                }
            });
        });
        // 编辑超级群
        form.on('submit(editUltragroupSubmit)', function(data){
            var data = data.field;
            var approval = $('#editApprovalId').prop('checked') ? 0 : 1;
            $.ajax({
                type: "post",
                async: false,
                url: [[${basePath}]] + "/ultragroup/updateUltragroup",
                data: {
                    ultragroupId: data.ultragroupId,
                    ultragroupName: data.groupName,
                    avatar: data.avatar,
                    approval: approval
                },
                success: function (res) {
                    if (res.success) {
                        layer.msg("编辑成功！", {icon: 1}, 2000);
                        layer.closeAll();
                        tableIns.reload();
                    } else {
                        layer.msg(res.msg, {icon: 2}, 2000);
                    }
                }
            });
        });

        //头工具栏事件
        table.on('toolbar(event)', function (obj) {
            switch (obj.event) {
                case 'toSave':
                    layer.open({
                        type: 1,
                        title: '新增',
                        area: ['700px', '570px'],
                        content: $('#ultragroupId'),
                        success: function () {
                        }
                    });
                    break;
            }
        });

        // 行编辑
        table.on('tool(event)', function (obj) {
            var data = obj.data;
            //清除登录token
            if (obj.event === 'delete') {
                layer.confirm("确定删除此条聊天记录?", {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        type: "post",
                        url: [[${basePath}]] + "/ultragroup/deleteUltragroup",
                        data: {
                            id: data.id,
                            ultragroupId: data.ultragroupId
                        },
                        success: function (res) {
                            if (res.success) {
                                layer.msg("删除完成", {icon: 1}, 500);
                                layer.close(index);
                                tableIns.reload();
                            }
                        }
                    });
                });
            }
            if (obj.event === 'edit') {
                layer.open({
                    type: 1,
                    title: '编辑',
                    area: ['700px', '570px'],
                    content: $('#editUltragroupId'),
                    success: function () {
                        $("input[name='ultragroupId']").val(data.id);
                        $("input[name='groupName']").val(data.ultragroupName);
                        $("input[name='avatar']").val(data.avatar);

                        // 动态更新checkbox的值 需要刷新form
                        $('#editApprovalId').prop('checked', data.approval == '0');
                        form.render('checkbox');
                        $('#demo2').html('<img style="width: 50px;height: 50px;" src="'+ data.avatar +'" class="layui-upload-img">')
                    }
                });

            }
            if (obj.event === 'freeze') {
                layer.confirm("确定" + (data.status == 0 ? "关闭" : "开启") + "超级群状态?", {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        type: "post",
                        url: [[${basePath}]] + "/ultragroup/updateUltragroup",
                        data: {
                            ultragroupId: data.id,
                            status: data.status == 0 ? 1 : 0},
                        success: function (res) {
                            if (res.success) {
                                layer.msg("操作完成", {icon: 1}, 500);
                                layer.close(index);
                                tableIns.reload();
                            }
                        }
                    });
                }, function (index) {
                });
            }
            if (obj.event === 'addAdmin'){
                layer.open({
                    type: 2,
                    title: '管理员列表',
                    shadeClose: true,
                    shade: 0.2,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['80%', '550px'],
                    content: [[${basePath}]] + '/ultragroup/ultragroupAdmin?ultragroupId=' + data.ultragroupIdNum
                });
            }
            if (obj.event === 'memberList'){
                layer.open({
                    type: 2,
                    title: '超级群成员列表',
                    shadeClose: true,
                    shade: 0.2,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['80%', '550px'],
                    content: [[${basePath}]] + '/ultragroup/ultragroupMember?ultragroupId=' + data.ultragroupIdNum
                });
            }

        });

        function getJson() {
            return {
                where: {
                    sendName: $("input[name='name']").val()
                }
            };
        }
        $("#search").click(function () {
            tableIns.reload({
                where: {
                    sendName: $("input[name='name']").val()
                },
                page: {
                    curr: 1
                }
            });
        })
    });

    function examine(content) {
        layer.open({
            type: 1,
            title: '查看图片',
            shadeClose: true,
            shade: 0.2,
            maxmin: true, //开启最大化最小化按钮
            area: ['800px', '600px'],
            content: '<img src='+content+'>' //这里content是一个普通的String
        });
    }
</script>
<!-- 表格操作按钮集 -->
<script type="text/html" id="barOption">
    <a class="layui-btn layui-btn-xs" lay-event="addAdmin"><i class="layui-icon layui-icon-add"></i>管理员</a>
    <a class="layui-btn layui-btn-xs" lay-event="memberList"><i class="layui-icon layui-icon-add"></i>成员</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-add"></i>编辑</a>
    {{#  if(d.status == 1){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="freeze"><i class="layui-icon"></i>开启</a>
    {{#  } else { }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="freeze"><i class="layui-icon"></i>关闭</a>
    {{#  } }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
</body>
</html>